package com.jiuyou.clientController.mall.integral;

import com.jiuyou.clientController.base.ClientBaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.shopping.domain.MallIntegral;
import com.ruoyi.shopping.domain.MallIntegralRecord;
import com.ruoyi.shopping.service.IMallIntegralRecordService;
import com.ruoyi.shopping.service.IMallIntegralService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

/**
 * @author chengliang
 * @date 2022/7/27
 **/
@RestController
@RequestMapping("/client/mall/integral")
public class MallIntegralClientController extends ClientBaseController {
    @Autowired
    private IMallIntegralService integralService;

    @Autowired
    private IMallIntegralRecordService recordService;
    @RequestMapping("/getTotal")
    public AjaxResult getTotal(){
        MallIntegral mallIntegral = new MallIntegral();
        mallIntegral.setUserId(getLoginMemberId());
        List<MallIntegral> mallIntegrals = integralService.selectMallIntegralList(mallIntegral);
        BigDecimal zero = BigDecimal.ZERO;
        if(mallIntegrals.size()>0){
            zero = mallIntegrals.get(0).getIntegralBalance();
        }
        return AjaxResult.success(zero);
    }
    @RequestMapping("/getList")
    public AjaxResult getList(){
        startPage();
        MallIntegralRecord record = new MallIntegralRecord();
        record.setUserId(getLoginMemberId());
        List<MallIntegralRecord> mallIntegralRecords = recordService.selectMallIntegralRecordList(record);
        return AjaxResult.success(getDataTable(mallIntegralRecords));
    }
    @RequestMapping("/getTabData")
    public AjaxResult getTabData(){
        HashMap<String, Object> map = new HashMap<>(2);
        MallIntegralRecord record = new MallIntegralRecord();
        record.setUserId(getLoginMemberId());
        List<MallIntegralRecord> mallIntegralRecords = recordService.selectMallIntegralRecordList(record);
        BigDecimal point = BigDecimal.ZERO;
        BigDecimal consume = BigDecimal.ZERO;
        for (MallIntegralRecord mallIntegralRecord : mallIntegralRecords) {
            if("in".equals(mallIntegralRecord.getHandleDirection())){
                point =  point.add(mallIntegralRecord.getHandleIntegral());
                consume = consume.add(mallIntegralRecord.getIntegralBalance());
            }
            if("out".equals(mallIntegralRecord.getHandleDirection())){

            }
        }
        map.put("point",point);
        map.put("consume",consume);
        return AjaxResult.success(map);
    }
}
